#include "buttonrelayGP.h"
#include <QPainter>

ButtonGP::ButtonGP(Button& button,QGraphicsItem *parent, int lampradius)
: QGraphicsWidget(parent)
{
    setObjectName("ButtonGP");
    setAcceptHoverEvents(true);
    setCacheMode(DeviceCoordinateCache);
    mPixON = QPixmap(":/images/circle-green.png");
    mPixOFF = QPixmap(":/images/circle-gray.png");
    mPixUnknown = QPixmap(":/images/circle-red.png");
    mLampRadius = lampradius;
    _pix = mPixOFF;
    {
        mButton.mCId = button.mCId;
        mButton.mBId = button.mBId;
        mButton.mRId = button.mRId;
        mButton.mName = button.mName;
        mButton.mNotUsed = button.mNotUsed;

        mButton.mPos = button.mPos;
        mButton.mX = button.mX;
        mButton.mY = button.mY;
        mButton.mCX = button.mCX;
        mButton.mCY = button.mCY;

        mButton.mRType = button.mRType;
        mButton.mStatus = button.mStatus;

        mButton.mWidget = button.mWidget;
        mButton.mWidgetSub = button.mWidgetSub;
    }
    setStatus(mButton.mStatus, button.mStime, button.mEtime);
}

void ButtonGP::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
{
    bool down = option->state & QStyle::State_Sunken;
    QRectF r = boundingRect();
    QLinearGradient grad(r.topLeft(), r.bottomRight());
    grad.setColorAt(down ? 1 : 0, option->state & QStyle::State_MouseOver ? Qt::black : Qt::lightGray);
    grad.setColorAt(down ? 0 : 1, Qt::darkGray);
    painter->setPen(Qt::white);
    painter->setBrush(grad);
    painter->drawEllipse(r);
    QLinearGradient grad2(r.topLeft(), r.bottomRight());
    grad.setColorAt(down ? 1 : 0, Qt::darkGray);
    grad.setColorAt(down ? 0 : 1, Qt::lightGray);
    painter->setPen(Qt::NoPen);
    painter->setBrush(grad);
    if (down)
        painter->translate(2, 2);
    painter->drawEllipse(r.adjusted(5, 5, -5, -5));
//    painter->drawPixmap(-_pix.width()/2, -_pix.height()/2, _pix);
    painter->drawPixmap(-mLampRadius+4, -mLampRadius+4, mLampRadius*2-6, mLampRadius*2-6, _pix);
}

void ButtonGP::mousePressEvent(QGraphicsSceneMouseEvent *)
{
    emit pressed();
    update();
}

void ButtonGP::mouseReleaseEvent(QGraphicsSceneMouseEvent *)
{
    update();
}

void ButtonGP::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{
    if(QEvent::GraphicsSceneHoverEnter == event->type())
    {
        emit hoverenter();
    }
}

void ButtonGP::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
{
    if(QEvent::GraphicsSceneHoverLeave == event->type())
    {
        emit hoverleave();
    }
}

const Button *ButtonGP::button()
{
    return &mButton;
}

void ButtonGP::setStatus(int status, uint stime, uint etime)
{
    mONStartTime = stime;
    mONElapseTime = etime;

    switch (status) {
    case RELAY_STATUS_OFF:
        _pix = mPixOFF;
        break;
    case RELAY_STATUS_ON:
        if(0 == mButton.mRType)
          _pix = mPixON;
        break;
    case RELAY_STATUS_AIR:
    case RELAY_STATUS_COOLING:
    case RELAY_STATUS_HEATING:
    case RELAY_STATUS_PENDING:
    case RELAY_STATUS_UNKNOWN:
    case RELAY_STATUS_NOTUSED:
    default:
         _pix = mPixUnknown;
        break;
    }
    mButton.mStatus = status;
    update();
}
